package com.masiyi.aliyun.config;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.masiyi.aliyun.template.AliyunTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(value = AliyunOssProperties.class)
public class AliyunOssAutoConfiguration {


    @Autowired
    private AliyunOssProperties aliyunOssProperties;


    @Bean
    public OSS getAliyun() {
        // Endpoint以华东1（杭州）为例，其它Region请按实际情况填写。
        String endpoint = aliyunOssProperties.getEndpoint();
        String accessKeyId = aliyunOssProperties.getAccessKeyId();
        String accessKeySecret = aliyunOssProperties.getAccessKeySecret();
        return new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    }

    @Bean
    public AliyunTemplate getAliyunTemplate(OSS oss) {
        return new AliyunTemplate(oss, aliyunOssProperties);
    }

}
